package com.sgai.zero_robot.pop;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.sgai.zero_robot.R;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.core.callback.CameraCallback;


public class SettingCameraView extends FrameLayout implements View.OnClickListener {
    private final String TAG = "CameraView";

    private EditText etInAngle, etOutAngle, etMidAngle;
    private TextView btInSet, btMidSet, btOutSet;

    public SettingCameraView(@NonNull Context context) {
        this(context, null);
    }

    public SettingCameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SettingCameraView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = inflate(context, R.layout.setting_camera, this);
        etInAngle = view.findViewById(R.id.et_in_angle);
        etMidAngle = view.findViewById(R.id.et_mid_angle);
        etOutAngle = view.findViewById(R.id.et_out_angle);
        btInSet = view.findViewById(R.id.bt_in_set);
        btMidSet = view.findViewById(R.id.bt_mid_set);
        btOutSet = view.findViewById(R.id.bt_out_set);
        btInSet.setOnClickListener(this);
        btMidSet.setOnClickListener(this);
        btOutSet.setOnClickListener(this);
    }

    CameraCallback cameraCallback = new CameraCallback() {
        @Override
        public void onOut(String angle) {
            setValue(etOutAngle, RobotManagerFactory.getInstance().cameraOut);
        }

        @Override
        public void onMid(String angle) {
            setValue(etMidAngle, RobotManagerFactory.getInstance().cameraMid);
        }

        @Override
        public void onIn(String angle) {
            setValue(etInAngle, RobotManagerFactory.getInstance().cameraIn);
        }
    };

    private void setValue(EditText et, String value) {
        if (et != null && !TextUtils.isEmpty(value)) {
            et.setText(value);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow: ");
        setValue(etInAngle, RobotManagerFactory.getInstance().cameraIn);
        setValue(etMidAngle, RobotManagerFactory.getInstance().cameraMid);
        setValue(etOutAngle, RobotManagerFactory.getInstance().cameraOut);
        RobotManagerFactory.getInstance().addCameraCallback(cameraCallback);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow: ");
        RobotManagerFactory.getInstance().removeCamareCallback(cameraCallback);
    }

    @Override
    public void onClick(View v) {
        if (v == btInSet) {
            String string = etInAngle.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<1200||i>2200){
                Toast.makeText(getContext(), "摄像头内侧角度范围在1200-2200之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setCameraInAngle(i);
        } else if (v == btOutSet) {
            String string = etOutAngle.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<1200||i>2200){
                Toast.makeText(getContext(), "摄像头外侧角度范围在1200-2200之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setCameraOutAngle(i);
        } else if (v == btMidSet) {
            String string = etMidAngle.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<1200||i>2200){
                Toast.makeText(getContext(), "摄像头中间角度范围在1200-2200之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setCameraMidAngle(i);
        }
    }
}
